package cn.itcast.web.controller.stat;


import cn.itcast.service.stat.StatService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.AddressUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/stat")
public class StatController extends BaseController {

    @Reference
    private StatService statService;


    /*
          url:/stat/toCharts.do?chartsType=factory
          作用： 根据传入的参数进入不同的页面
          参数： 进入页面的不同类型
        */
    @RequestMapping("/toCharts")
    public String toCharts(String chartsType){

        return "stat/stat-"+chartsType;
    }
    /*
        异步请求返回  生产厂家销售情况  所需要的数据
        /stat/factoryCharts.do
     */
    @RequestMapping("/factoryCharts")
    @ResponseBody
    public List<Map<String,Object>> factoryCharts(){
        return statService.getFactoryData(getLoginUserCompanyId());
    }

    /*
        异步请求返回  产品销售排行  所需要的数据
        /stat/sellCharts.do
     */
    @RequestMapping("/sellCharts")
    @ResponseBody
    public List<Map<String,Object>> sellCharts(){
        return statService.getSellData(getLoginUserCompanyId());
    }

    /*
        异步请求返回  系统访问压力图  所需要的数据
        /stat/onlineCharts.do
     */
    @RequestMapping("/onlineCharts")
    @ResponseBody
    public List<Map<String,Object>> onlineCharts(){
        return statService.getOnlineData();
    }



    @RequestMapping("/getAddressData")
    @ResponseBody
    public Map<String, Object> getAddressData() throws IOException {

        List<Map<String, Object>> getAddressData = statService.findAddressData(getLoginUserCompanyId());

        List<String> city = new ArrayList<>();

        for (Map<String, Object> getAddressDatum : getAddressData) {
            String name = (String)getAddressDatum.get("name");

            city.add(name);
        }

        Map<String, List<Float>> cityAddress = AddressUtils.getCityAddress(city);

        Map<String, Object> map = new HashMap<>();
        map.put("data",getAddressData);
        map.put("coordMap",cityAddress);

        return map;
    }
}
